<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>商城购物车</title>
    <%@include file="../common/header.jsp" %>
</head>
<body>
<%@include file="../common/top.jsp" %>
<form action="${pathUrl}/home/cart/indexList" method="post" id="cartForm">
<div class="section">
    <div class="cart-head">
        <h2>我的购物车</h2>
        <div class="cart-setp">
            <ul>
                <li class="first active">
                    <div class="progress">
                        <span>1</span>
                        放进购物车
                    </div>
                </li>
                <li>
                    <div class="progress">
                        <span>2</span>
                        填写订单信息
                    </div>
                </li>
                <li class="last">
                    <div class="progress">
                        <span>3</span>
                        支付/确认订单
                    </div>
                </li>
            </ul>
        </div>
    </div>
</div>
    <div id="loadDiv">
        <div class="section">
            <table class="cart-table" width="100%" cellspacing="0" cellpadding="8" border="0" align="center">
                <tbody>
                <tr>
                    <th colspan="2" align="left">商品信息</th>
                    <th width="84" align="left">单价</th>
                    <th width="104" align="center">数量</th>
                    <th width="104" align="left">金额(元)</th>
                    <th width="54" align="center">操作</th>
                </tr>
                <c:set var="priceSum" value="0"/>
                <c:forEach items="${userCartLogVos}" var="temp">
                    <tr>
                        <td width="68">
                            <a target="_blank" href="${pathUrl}/home/goods/detail?id=${temp.goods.id}">
                                <img onerror="this.src='${pathUrl}/public/images/nopic.png'" src="${pathUrl}${temp.goods.goodsImg}" class="img">
                            </a>
                        </td>
                        <td>
                            <a target="_blank" href="${pathUrl}home/goods?action=detail&amp;id=${temp.goods.id}">${temp.goods.goodsName}</a>
                            <p class="stxt"></p>
                        </td>
                        <td>
                            ${temp.goods.sellPrice}
                        </td>
                        <td align="center">
                            <div class="buy-box">
                                <input type="number"  class="input" min="1" value="${temp.userCartLog.goNum}" index="${temp.goods.id}" style="border: solid 1px #666;">
                            </div>
                        </td>
                        <td>
                <span class="red">
                    ￥<label name="amountCount">${temp.totalPrice}</label>
                    <c:set var="priceSum" value="${priceSum+temp.totalPrice}"/>
                </span>
                        </td>
                        <td align="center">
                            <a href="javascript:;" onclick="deleteOne(${temp.userCartLog.id})">删除</a>
                        </td>
                    </tr>
                </c:forEach>


                <tr>
                    <th colspan="6" align="right">
                        已选择商品 <b class="red" id="totalQuantity">${userCartLogVos.size()}</b> 件 &nbsp;&nbsp;&nbsp;
                        商品总金额（不含运费）：<span class="red">￥&nbsp;&nbsp;</span><b class="red" id="totalAmount">${priceSum}</b>元
                    </th>
                </tr>

                </tbody>
            </table>
        </div>

        <div class="cart-foot section">
            <div class="right-box">
                <button class="button" type="button" onclick="javascript:location.href='${pathUrl}/home/goods/index';">继续购物</button>
                <button class="button" style="background:#34ad2a" onclick="sumbitWirter()">立即结算</button>
            </div>
        </div>
    </div>
</form>
<jsp:include page="../../common/laypage.jsp"/>
<script type="text/javascript">
$(function(){
    var nums = $(".input");
    for(var i = 0;i<nums.length;i++){
        nums.eq(i).change(function(){
            var obj = $(this);
            if(obj.val()<1){
                obj.val(1);
            }

            var url="${pathUrl}/home/cart/changeNum";
            var id = obj.attr("index");
            var num = obj.val();

            var sellPrice =  obj.parent().parent().prev().html();

            var countPrice = obj.parent().parent().next().children().children();

            $.post(url,{id:id, num:num},function(result){
                if(result.status==1){
                    obj.parent();
                    countPrice.text(sellPrice*result.data);
                } else {
                    obj.val(result.data);
                    window.location.reload();
                }
            });

        });
    }

});


function deleteOne(id){
	var con = confirm("确定要删除该商品吗?");
	if(!con){
        return;
	}

	var url="${pathUrl}/home/cart/delete";
	$.post(url,{id:id},function(result){
	    if(result.status==1){
            alert(result.msg);
            window.location.reload();
        } else {
            alert(result.msg);
        }
	});
	
}
function sumbitWirter(){
	$("#cartForm").attr("action","${pathUrl}/home/cart/writeorder");
	$("#cartForm").submit();
}
</script>
</body>
</html>